﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Web.Security;
using MemberShip.Imagine.Models.Helpers;

namespace TVXD23.Web.Models
{
    public class cmsRepository
    {
        public TVXD23Entities db = new TVXD23Entities();
        private int Nums = 10;
        public TVXD23Entities DB
        {
            get { return db; }
            set { db = value; }
        }
        #region pressCategory
        public int GetPressCategoryId(string title)
        {
            var kq = db.PressCategories.Where(m => m.Title == title).FirstOrDefault();
            if (kq == null) return 0;
            return kq.PressCategoryId;
        }
        public void AddPressCategory(PressCategory model)
        {
            db.AddToPressCategories(model);
            db.SaveChanges();
        }

        public int DelPressCategory(int pressCategoryId)
        {
            var kq = GetPressCategory(pressCategoryId);
            if (kq == null)
                return 0;
            db.PressCategories.DeleteObject(kq);
            db.SaveChanges();
            return 1;
        }

        public PressCategory GetPressCategory(int pressCategoryId)
        {
            return db.PressCategories.FirstOrDefault(m => m.PressCategoryId == pressCategoryId);
        }
        public List<PressCategory> PressCategories()
        {
            return db.PressCategories.ToList();
        }
        #endregion

        #region Press
        public List<Press_TranslatedCopy> NewestPress_TranslatedCopy(int Nums)
        {
            return db.Press_TranslatedCopy.OrderByDescending(m => m.Id).Take(Nums).ToList();
        }
      
   
        public List<Press_TranslatedCopy> TopBanDichTheoDanhMuc(string tenDanhMuc,int soPhanTu)
        {
            List<Press_TranslatedCopy> lst = new List<Press_TranslatedCopy>();
            var ds = db.PressCategories.Where(m => m.Title.Contains(tenDanhMuc)).FirstOrDefault();
            var dt = ds.Presses.OrderByDescending(m => m.DateCreate).Take(soPhanTu).Select(m => m.PressId).ToList();
            foreach (var item in dt)
            {
                var kq = GetPressTranslated(item);
                if(kq!=null)
                lst.Add(kq);
            }
            return lst;

        }
        public List<Press_TranslatedCopy> TopBanDichTheoDanhMuc(int id, int soPhanTu)
        {
            List<Press_TranslatedCopy> lst = new List<Press_TranslatedCopy>();
            var ds =
                db.PressCategories.Where(m => m.PressCategoryId == id).FirstOrDefault().Presses.OrderByDescending(
                    t => t.DateCreate).Take(soPhanTu).ToList();
           
            foreach (var item in ds)
            {
                var kq = GetPressTranslated(item.PressId);
                if (kq != null)
                    lst.Add(kq);
            }
            return lst;

        }
        public bool IsTranslated(int pressId)
        {
            var d = db.Press_TranslatedCopy.Where(m => m.PressId == pressId).FirstOrDefault();
            if (d!=null) return true;
            else return false;
        }
        public List<Press > TopBaiVietTheoMaDanhMuc(int id,int sophantu)
        {
            return
                db.PressCategories.Where(m => m.PressCategoryId == id).FirstOrDefault().Presses.OrderByDescending(
                    m => m.DateCreate).Take(sophantu).ToList();
        }
        public List<Press_TranslatedCopy> TopBaiDichTheoMaDanhMuc(int id, int sophantu)
        {
            var lst=
                db.PressCategories.Where(m => m.PressCategoryId == id).FirstOrDefault().Presses.OrderByDescending(
                    m => m.DateCreate).Take(sophantu).ToList();
            List<Press_TranslatedCopy> ds = new List<Press_TranslatedCopy>();
            foreach(var item in lst)
            {
                var m = GetPressTranslatedByPressId(item.PressId);
                if(m!=null)
                    ds.Add(m);
                    
            }
            return ds;
        }
        public List<Press> TopBaiVietTheoDanhMuc(string tenDanhMuc,int soPhanTu)
        {
            var ds = db.PressCategories.Where(m => m.Title.Contains(tenDanhMuc)).FirstOrDefault();
            return ds.Presses.OrderByDescending(m => m.DateCreate).Take(soPhanTu).ToList();
        }
        public List<Press> TopBaiVietTheoDanhMuc(int id, int soPhanTu)
        {
            var ds =
               db.PressCategories.Where(m => m.PressCategoryId == id).FirstOrDefault().Presses.OrderByDescending(
                   t => t.DateCreate).Take(soPhanTu).ToList();
            return ds;
        }
        public Press GetPress(string title)
        {
            var kq = db.Presses.Where(m => m.Title.Contains(title)).FirstOrDefault();
            return kq;
        }
        public Press GetFirstPressByCategoryId(int id)
        {
            var kq =
                db.Presses.Where(m => m.PressCategoryId == id).OrderByDescending(t => t.DateCreate).FirstOrDefault();
            return kq;
        }
        public Press_TranslatedCopy GetFirstPressTranslatedCopyByCategoryId(int id)
        {
            var kq =
               db.Presses.Where(m => m.PressCategoryId == id).OrderByDescending(t => t.DateCreate).FirstOrDefault();
            Press_TranslatedCopy result=new Press_TranslatedCopy();
            if(kq!=null)
            {
               result = db.Press_TranslatedCopy.Where(m => m.PressId == kq.PressId).FirstOrDefault();
            }
            return result;
        }
        public Press_TranslatedCopy GetPressTranslated(string title)
        {
            var kq = db.Press_TranslatedCopy.Where(m => m.Title.Contains(title)).FirstOrDefault();
            return kq;
        }
        public bool IsPressOwner(int pressId, string userName)
        {
            var kq = db.Presses.Where(m => m.PressId == pressId && m.UserNameCreate.Trim() == userName.Trim()).FirstOrDefault();

            if (kq == null) return false;
            return true;
        }
        public Press GetPress(int pressId)
        {
            return db.Presses.FirstOrDefault(m => m.PressId == pressId);
        }
        public void AddPress(Press model)
        {
            db.AddToPresses(model);
            db.SaveChanges();
        }
        public int DelPress(int pressId)
        {
            var s = GetPress(pressId);
            if (s == null) return 0;
            db.Presses.DeleteObject(s);
            db.SaveChanges();
            return 1;
        }
        public void EditPress(Press model)
        {
            var s = GetPress(model.PressId);
            s.Title = model.Title;
            s.DateCreate = model.DateCreate;
            s.Body = model.Body;
            s.Description = model.Description;
            s.ImagePath = model.ImagePath;
            s.PressCategoryId = model.PressCategoryId;
            s.PictureCategoryId = model.PressCategoryId;
            s.UserNameCreate = model.UserNameCreate;
            s.ViewCount = model.ViewCount;

            db.SaveChanges();
        }
        public Press_TranslatedCopy GetPressTranslated(int Id)
        {
            //tim theo khoa cua ban goc
            var kq = db.Press_TranslatedCopy.Where(m => m.Id == Id).FirstOrDefault();
            return kq;
        }
        public Press_TranslatedCopy GetPressTranslatedByPressId(int Id)
        {
            //tim theo khoa cua ban goc
            var kq = db.Press_TranslatedCopy.Where(m => m.PressId == Id).FirstOrDefault();
            return kq;
        }
        //get top 5 bai viet dich moi nhat trong cung danh muc
        public List<Press> Presses(int pressCategoryId,int page)
        {
            var ds=
                db.PressCategories.Where(m => m.PressCategoryId == pressCategoryId).FirstOrDefault().Presses.
                    OrderByDescending(m => m.DateCreate);
            return new PagedList<Press>(ds, page, Nums).ToList();
        }
        public List<Press_TranslatedCopy> Presses_TranslatedCopy(int pressCategoryId, int page)
        {
            var ds =
                db.PressCategories.Where(m => m.PressCategoryId == pressCategoryId).FirstOrDefault().Presses.
                    OrderByDescending(m => m.DateCreate);
           var ds2=new  PagedList<Press>(ds, page, Nums).ToList();
            var lst = new List<Press_TranslatedCopy>();
            foreach(var item in ds2)
            {
                var kq = GetPressTranslated(item.PressId);
                if(kq!=null)
                    lst.Add(kq );
            }
            return lst;
        }
        public bool IsPressTranslatedOwner(int id,string userName)
        {
            var d = db.Press_TranslatedCopy.Where(m=>m.Id==id && m.UserNameTranslated.Trim() == userName.Trim()).FirstOrDefault();
            if(d==null) return false;
            return true;
        }
        public List<Press> Presses(int page)
        {
            return new PagedList<Press>(db.Presses.ToList(), page, Nums).ToList();
        }
        public List<Press> PressesByCategory(int page, int pressCategoryId)
        {
            var lst = new PagedList<Press>(db.Presses.Where(m => m.PressCategoryId == pressCategoryId).OrderByDescending(m => m.DateCreate), page, Nums).ToList();
            return lst;
        }
       
        public List<Press> NewestPress(int Nums)
        {
            return db.Presses.OrderByDescending(m => m.DateCreate).Take(Nums).ToList();
        }
      

        public string LimitString(string st, int nums)
        {
            if (st.Length <= nums) return st;
            return st.Substring(0, nums) + " ...";
        }
        #endregion

        public void AddPressTranslatedCopy(Press_TranslatedCopy press)
        {
            db.Press_TranslatedCopy.AddObject(press);
            db.SaveChanges();
        }

        public bool DeleteMember(string userName)
        {
            MembershipProvider pr = Membership.Provider;
            int i = db.SaveChanges();
            bool a = pr.DeleteUser(userName, deleteAllRelatedData: true);
            if (a && i != 0)
                return true;
            else
            {
                return false;
            }
        }
        public void ChangePass(string userName,string newPass)
        {
            MembershipProvider pr = Membership.Provider;
           string k= Membership.GetUser(userName).GetPassword();
            pr.ChangePassword(userName, k, newPass);
        }

        public void DeletePressTranslatedCopy(int id)
        {
            Press_TranslatedCopy ms = db.Press_TranslatedCopy.Where(m => m.Id == id).FirstOrDefault();
            db.Press_TranslatedCopy.DeleteObject(ms);
            db.SaveChanges();
        }
    }
}